package cn.com.liuwenquan.flex.business.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import cn.com.liuwenquan.flex.business.Delegates.CustomerDelegate;
	import cn.com.liuwenquan.flex.events.CustomerEvent;
	import cn.com.liuwenquan.flex.model.CustomerModelLocator;
	import cn.com.liuwenquan.flex.vo.Customer;
	import cn.com.liuwenquan.flex.vo.Person;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	
	public class CustomerCommand implements ICommand, IResponder
	{
		public function CustomerCommand()
		{
		}
		public function execute(event:CairngormEvent):void
		{
//			var responder:Responder = new Responder(test,null);
			var customer:CustomerEvent = CustomerEvent(event);
			//创建所需要的代理，连到Web端
			var cService :CustomerDelegate =  new CustomerDelegate(this);
			//处理了代理的响应？触发监听获得结果
			cService.getCustomerInfo();
		}
		
//		public function test(evt:ResultEvent):void{
//			trace("--------------------"+evt.result.length);
//		}
		
		public function result(event:Object):void
		{   
		    if(event.result != null && event.result.length > 0){
		    	trace("--------------------"+event.result.length);
		    	var info:ArrayCollection = event.result as ArrayCollection;
			    CustomerModelLocator.getInstance().customer_info = info;
			    for(var i:int=0; i<event.result.length; i++){
//			    	CustomerModelLocator.getInstance().customer.addItem(info[i] as Customer);
                    var customer:Customer = new Customer();
                    customer.customerId = info[i].customerId;
                    customer.companyName = info[i].companyName;
                    customer.createDate = info[i].createDate;
                    customer.fax = info[i].fax;
                    customer.flag = info[i].flag;
                    customer.phone = info[i].phone;
                    customer.shortName = info[i].shortName;
                    customer.person = new ArrayCollection();
                    
			    	for(var j:int=0; j<info[i].personList.length; j++){
			    		var person:Person = new Person();
			    		person.callphone = info[i].personList[j].callphone;
			    		person.createDate = info[i].personList[j].createDate;
			    		person.email = info[i].personList[j].email;
                        person.fax = info[i].personList[j].fax;
                        person.pads = info[i].personList[j].pads;
                        person.personId = info[i].personList[j].personId;
                        person.personName = info[i].personList[j].personName;
                        person.telephone = info[i].personList[j].telephone;
                        person.updateDate = info[i].personList[j].updateDate;
                        person.customer = info[i].personList[j].customer;
                        
                        customer.person.addItem(person);
			    	}
                    CustomerModelLocator.getInstance().customer_person.addItem(customer);
			    	trace("---------:"+CustomerModelLocator.getInstance().customer_person[i].companyName);
//			    	trace(" |-------------"+CustomerModelLocator.getInstance().customer_person[i].person[0].personName);
			    }
			    trace("----------获得内容成功-----------");
		    }else{
		    	trace("-------------no customer info please add-----------");
		    }
		}
		
		public function fault(info:Object):void
		{
            trace("------get customer info filse-------:"+info.valueOf());
			Alert.show("Menu data Fail Error ");
		}

	}
}